package com.gzx.plugin.tjzy.modular.gzxcase.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.gzx.plugin.tjzy.modular.gzxcase.dto.GzxAllocateCaseDto;
import com.gzx.plugin.tjzy.modular.gzxcase.entity.CaseQueryParam;
import com.gzx.plugin.tjzy.modular.gzxcase.entity.GzxCase;
import com.gzx.plugin.tjzy.modular.gzxcase.entity.GzxCaseDetail;
import com.gzx.plugin.tjzy.modular.gzxcase.mapper.GzxCaseDetailMapper;
import com.gzx.plugin.tjzy.modular.gzxcase.mapper.GzxCaseMapper;
import com.gzx.plugin.tjzy.modular.gzxcase.service.GzxCaseDbService;
import com.gzx.plugin.tjzy.modular.gzxcase.vo.GzxCaseDetailVo;
import com.gzx.plugin.tjzy.modular.gzxcase.vo.GzxCaseVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.consts.IntConstant;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonDateUtil;
import vip.xiaonuo.common.util.CommonSleepUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/5/23 09:13
 */
@Service
@RequiredArgsConstructor
@DS("tjzy")
public class GzxCaseDbServiceImpl implements GzxCaseDbService {

    private final GzxCaseMapper caseMapper;

    private final GzxCaseDetailMapper caseDetailMapper;

    /**
     * 案件分页查询
     *
     * @param page               分页参数
     * @param lambdaQueryWrapper 查询条件
     * @return
     */
    @Override
    @DS("tjzy")
    public Page<GzxCaseVo> pageList(Page page, LambdaQueryWrapper<GzxCase> lambdaQueryWrapper) {
        return caseMapper.selectVoPage(new Page<>(page.getCurrent(), page.getSize()), lambdaQueryWrapper);
    }

    /**
     * 案件详情分页查询
     *
     * @param page               分页参数
     * @param lambdaQueryWrapper 查询条件
     * @return
     */
    @Override
    @DS("tjzy")
    public Page<GzxCaseDetailVo> pageListDetail(Page page, LambdaQueryWrapper<GzxCaseDetail> lambdaQueryWrapper) {
        return caseDetailMapper.selectVoPage(new Page<>(page.getCurrent(), page.getSize()), lambdaQueryWrapper);
    }

    /**
     * 查询案件ID和甲方系统主键
     *
     * @param caseQueryParamList
     * @return
     */
    @Override
    @DS("tjzy")
    public List<GzxCase> listCasesByBatchAndDebtorIds(List<CaseQueryParam> caseQueryParamList) {
        return caseMapper.listCasesByBatchAndDebtorIds(caseQueryParamList);
    }

    /**
     * 查询案件
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    @DS("tjzy")
    public GzxCase selectCase(LambdaQueryWrapper<GzxCase> lambdaQueryWrapper) {
        return caseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 查询案件
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    @DS("tjzy")
    public GzxCaseVo selectCaseVo(LambdaQueryWrapper<GzxCase> lambdaQueryWrapper) {
        return caseMapper.selectVoOne(lambdaQueryWrapper);
    }

    /**
     * 查询案件
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    @DS("tjzy")
    public List<GzxCase> selectCaseList(LambdaQueryWrapper<GzxCase> lambdaQueryWrapper) {
        return caseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 查询案件
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    @DS("tjzy")
    public List<GzxCaseVo> selectVoCaseList(LambdaQueryWrapper<GzxCase> lambdaQueryWrapper) {
        return caseMapper.selectVoList(lambdaQueryWrapper);
    }

    /**
     * 查询案件详情
     *
     * @param lambdaQueryWrapper 查询条件
     * @return
     */
    @Override
    @DS("tjzy")
    public List<GzxCaseDetail> selectCaseDetailList(LambdaQueryWrapper<GzxCaseDetail> lambdaQueryWrapper) {
        return caseDetailMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 案件修改
     *
     * @param lambdaUpdateWrapper
     */
    @Override
    @DS("tjzy")
    public void update(LambdaUpdateWrapper<GzxCase> lambdaUpdateWrapper) {
        caseMapper.update(lambdaUpdateWrapper);
    }

    /**
     * 根据ID修改案件
     *
     * @param gzxCase
     */
    @Override
    @DS("tjzy")
    public void updateById(GzxCase gzxCase) {
        caseMapper.updateById(gzxCase);
    }

    /**
     * 批量保存案件
     *
     * @param caseList
     */
    @Override
    @DS("tjzy")
    public void saveBatch(List<GzxCase> caseList) {
        String currentTime = CommonDateUtil.getCurrentTimeStr();
        List<List<GzxCase>> batchList = Lists.partition(caseList, IntConstant.VALUE_1000);
        for (List<GzxCase> batch : batchList) {
            String batchSql = batch.stream()
                    .map(s -> String.format("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
                            escapeSql(s.getCompanyCode()),
                            escapeSql(s.getProductCode()),
                            escapeSql(s.getProjectCode()),
                            escapeSql(s.getBatchNumber()),
                            escapeSql(String.valueOf(s.getCaseStatus())),
                            escapeSql(s.getDebtorName()),
                            escapeSql(String.valueOf(s.getDebtorSex())),
                            escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getDebtorPhone())),
                            escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getDebtorCardId())),
                            escapeSql(String.valueOf(s.getIntentTag())),
                            escapeSql(String.valueOf(s.getPenaltyInterest())),
                            escapeSql(String.valueOf(s.getLoanAmount())),
                            escapeSql(String.valueOf(s.getOverdueAmount())),
                            escapeSql(String.valueOf(s.getCleanLoanAmount())),
                            escapeSql(String.valueOf(s.getOverdueInterest())),
                            escapeSql(String.valueOf(s.getOverdueFees())),
                            escapeSql(String.valueOf(s.getOverdueStatus())),
                            escapeSql(String.valueOf(s.getMediatorId())),
                            escapeSql(s.getMediatorName()),
                            escapeSql(String.valueOf(s.getAffiliatedCompany())),
                            escapeSql(s.getPartnerBusinessId()),
                            escapeSql(s.getTenantId()),
                            escapeSql(currentTime)
                    ))
                    .collect(Collectors.joining(", "));
            caseMapper.insertBatch(batchSql.replace("'null'", "null"));
            CommonSleepUtil.sleep100();
        }
    }

    /**
     * 批量保存案件明细
     *
     * @param caseDetailList
     */
    @Override
    @DS("tjzy")
    public void saveBatchDetail(List<GzxCaseDetail> caseDetailList) {
        String currDate = CommonDateUtil.getCurrentTimeStr();
        List<List<GzxCaseDetail>> batchList = Lists.partition(caseDetailList, IntConstant.VALUE_1000);
        for (List<GzxCaseDetail> batch : batchList) {
            String batchSql = batch.stream()
                    .map(s -> String.format("('%s', '%s', '%s', '%s', '%s','%s','%s','%s', '%s', '%s')",
                            escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getDebtorCardId())),
                            escapeSql(s.getBatchNumber()),
                            escapeSql(String.valueOf(s.getLoanAmount())),
                            escapeSql(String.valueOf(s.getOverdueAmount())),
                            escapeSql(s.getOverdueDate()),
                            escapeSql(s.getContractNo()),
                            escapeSql(s.getPartnerBusinessId()),
                            escapeSql(s.getTenantId()),
                            escapeSql(s.getCreateUser()),
                            escapeSql(currDate)
                    ))
                    .collect(Collectors.joining(", "));
            caseDetailMapper.insertBatch(batchSql.replace("'null'", "null"));
            CommonSleepUtil.sleep100();
        }
    }

    /**
     * 分案修改案件主表数据
     *
     * @param allocateCaseDto
     */
    @Override
    @DS("tjzy")
    public void allocateCase(GzxAllocateCaseDto allocateCaseDto) {
        caseMapper.allocateCase(allocateCaseDto);
    }
}
